/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.garagesale.config.hibernate;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author christophe
 */
public class HibernateUtil {

    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {

        if (null == sessionFactory) {
            sessionFactory = initializeSessionFactory();
        }
        return sessionFactory;
    }

    private static SessionFactory initializeSessionFactory() {
        SessionFactory sessionFactory = null;
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml)
            // config file.
            sessionFactory = new AnnotationConfiguration().configure("com/garagesale/config/hibernate/hibernate.cfg.xml").buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception.
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        return sessionFactory;
    }
}
